import { AppComponentBase } from '@shared/common/app-component-base';
import { Component, OnInit, Injector } from '@angular/core';
import { ServiceProxy, DictionaryDTO } from '@shared/service-proxy/service-proxies';
import { FormBuilder, FormGroup, Validators } from '@angular/forms';

@Component({
  selector: 'app-state-abnormality',
  templateUrl: './state-abnormality.component.html',
  styleUrls: ['./state-abnormality.component.less']
})
export class StateAbnormalityComponent extends AppComponentBase implements OnInit {
  dataSet = [];
  dictionary: DictionaryDTO = new DictionaryDTO();
  isVisible: boolean = false;
  validateForm: FormGroup;
  isOkLoading: boolean = false;
  constructor(injector:Injector, private fb: FormBuilder, 
    private _service: ServiceProxy,
    ) {
      super(injector)
     }

  ngOnInit() {
    this.validateForm = this.fb.group({
      itemValue: [null, [Validators.required]]
    });
    this.getAbnormality();
  }

  getAbnormality() {
    this._service.getDictionaryByDictionaryTypeUsingGET("abnormality_rule").subscribe(result => {
      this.dataSet = result.result.fieldList;
    })
  }

  showModal(id: string): void {
    this.dictionary = new DictionaryDTO();
    if (id) {
      this._service.getDictionaryUsingGET(id).subscribe(result => {
        this.dictionary = result.result;
      })
    }
    this.isVisible = true;
  }

  handleOk(): void {
    this.isOkLoading = true;
    for (const i in this.validateForm.controls) {
      this.validateForm.controls[i].markAsDirty();
      this.validateForm.controls[i].updateValueAndValidity();
    }
    if (this.validateForm.valid) {
      this._service.updateDictionaryUsingPUT(this.dictionary.id, this.dictionary).subscribe(result => {

         if (result.success) {
          this.getAbnormality();
          this.message.success(this.l("save_success"));
          this.isVisible =false;
        }
        else
          this.message.info(this.l(result.error.message));

        this.isOkLoading = false;
        
      })

    }
    else {
      this.isOkLoading = false;
    }

  }

  handleCancel(): void {
    this.isVisible = false;
  }
}
